package com.controller;

import com.bean.*;
import com.bean.dangBean.Employee;
import com.service.ActivationRecordService;
import com.service.CommodityService;
import com.service.MarketServices;
import com.service.TypeService;
import com.util.DateUtil;
import com.util.PageAssistant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 界面业务控制器
 */
@Controller
@RequestMapping("activationRecord")
public class ActivationRecordController {

    @Autowired
    private ActivationRecordService activationRecordService;
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private MarketServices marketServices;
    @Autowired
    private TypeService typeService;


    @RequestMapping("set")
    public String setActivity(){
        System.out.println();
        System.out.println(">>>>   activationRecord  set");
        System.out.println("----------------------------------------");

        return "activationRecord/setActivity";
    }

    @RequestMapping("activity")
    public String activity(Model model, String choose, String typeId, String state,
                           PageAssistant<Market> paMar, HttpSession session){
        System.out.println();
        System.out.println(">>>>   activationRecord  activity");
        System.out.println("----------------------------------------");

        System.out.println("choose : " + choose);
        System.out.println(typeId);
        System.out.println(state);
        Map<String,Object> map = new HashMap<>();

        if (paMar == null){
            paMar = new PageAssistant<>();
        }

        List<Type> typeList = typeService.find(new Type());

        if (typeId != null && !typeId.equals("0")){
            map.put("typeId",typeId);
        }
        if (state != null && !state.equals("2")){
            map.put("state",state);
        }

        paMar.setCount(marketServices.count(map));

        map.put("start",paMar.getStart());
        map.put("size",paMar.getSize());

        if (paMar.getColumn() == null){
            paMar.setColumn("m_state");
            paMar.setState("asc");
        }
        map.put("column",paMar.getColumn() + " " + paMar.getState());

        List<Market> query = marketServices.query(map);

        System.out.println(query);

        paMar.setItems(query);
        System.out.println(paMar);
        model.addAttribute("names","全场商品打折");
        model.addAttribute("paMar",paMar);
        model.addAttribute("typeId",typeId);
        model.addAttribute("state",state);
        session.setAttribute("choose",Integer.parseInt(choose));
        session.setAttribute("typeList",typeList);

        return "/activationRecord/activityList";
    }


    @RequestMapping("discount")
    public String discount(String choose, Model model){
        System.out.println();
        System.out.println(">>>>   activationRecord  discount");
        System.out.println("----------------------------------------");
        model.addAttribute("choose",choose);

        return "/activationRecord/discount";
    }

    @RequestMapping("add")
    @ResponseBody
    public Map<String,Object> add(String commodityType, String discount, String typeId,HttpSession session) throws Exception {
        System.out.println();
        System.out.println(">>>>   activationRecord  add");
        System.out.println("----------------------------------------");

        System.out.println("commodityType  : " + commodityType);
        System.out.println("discount  : " + discount);
        System.out.println("typeId  : " + typeId);

        Map<String,Object> map = new HashMap<>();

        /* 活动折扣 */
        Double discounts = Double.valueOf(discount)/10;

        if (commodityType != null && !commodityType.equals("2")){
            map.put("state",commodityType);
        }

        if (typeId != null){
            map.put("typeId",typeId);
        }

        List<Market> marketList = marketServices.query(map);

        String date = DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss");
        Date nowTime = DateUtil.parse(date);

        Employee employee = (Employee)session.getAttribute("loginInfo");

        StringBuffer sb = null;
        int chooses = (int)session.getAttribute("choose");

        if (chooses == 1){
            sb = new StringBuffer(
            "-----------------  活动详情  -------------------\n"
                    + " 创建时间：" + DateUtil.format(nowTime) + "\n"
                    + " 发起人：" + employee.getRealName() + "\n"
                    + " 折扣：" + discount + "折\n"
                    + " 商品类型：" + (commodityType.equals("0")?"普通商品":(commodityType.equals("1")?"活动商品":"所有商品")) + "\n\n"
                    + "-----------------  商品详情  -------------------\n"
                    + "序号\t商品名称\t商品销售价\t原活动价\t现活动价\n"
            );
        }else if (chooses == 2){
            List<Type> typeList = typeService.find(new Type(Integer.valueOf(typeId)));
            sb = new StringBuffer(
            "-----------------  活动详情  -------------------\n"
                    + " 创建时间：" + DateUtil.format(nowTime) + "\n"
                    + " 发起人：" + employee.getRealName() + "\n"
                    + " 折扣：" + discount + "折\n"
                    + " 商品类型：" + typeList.get(0).getStateName() + "\n\n"
                    + "-----------------  商品详情  -------------------\n"
                    + "序号\t商品名称\t商品销售价\t原活动价\t现活动价\n"
            );
        }

        int i = 1;
        for (Market m: marketList){
            sb.append(i + "\t" + m.getCommodity().getName()+ "\t" + m.getPrice()
                    + "\t" + m.getDiscountPrice() + "\t" + (m.getPrice()*discounts) + "\n");
            m.setDiscountPrice((m.getPrice()*discounts));
            m.setState(1);
            marketServices.modify(m);
            i++;
        }
        sb.append("-----------------------------------------------\n");

        if (marketList.size() > 0){
            ActivationRecord activationRecord = new ActivationRecord();
            activationRecord.setCreateTime(nowTime);
            activationRecord.setEmployeeId(employee.getId());
            activationRecord.setActive(sb.toString());
            activationRecordService.save(activationRecord);
        }

        session.removeAttribute("choose");
        session.setAttribute("currUrl","/activationRecord/selectActivity");

        map.put("msg","发起成功！");
        map.put("backUrl","/main");

        return map;
    }


    @RequestMapping("selectActivity")
    public String selectActivity(){
        System.out.println();
        System.out.println(">>>>   activationRecord  selectActivity");
        System.out.println("----------------------------------------");

        return "/activationRecord/selectActivity";
    }



    @RequestMapping("selectList")
    public String selectList(SelectDate selectDate, Model model,
                             PageAssistant<ActivationRecord> paAct) throws Exception {
        System.out.println();
        System.out.println(">>>>   activationRecord  selectList");
        System.out.println("----------------------------------------");

        if (paAct == null){
            paAct = new PageAssistant<>();
        }
        Map<String,Object> map = new HashMap<>();

        if (!selectDate.getTimeState().equals("0")){
            System.out.println("少时诵诗书所所所");

            Date now = new Date();
            Long millisecond = now.getTime();
            Long sevenDayMillisecond = null;

            switch (selectDate.getTimeState()){
                case "2":
                    sevenDayMillisecond= (long)7*24*60*60*1000;
                    break;
                case "3":
                    sevenDayMillisecond= (long)31*24*60*60*1000;
                    break;
                case "4":
                    sevenDayMillisecond= (long)62*24*60*60*1000;
                    break;
                case "5":
                    sevenDayMillisecond= (long)186*24*60*60*1000;
                    break;
            }

            millisecond = millisecond - sevenDayMillisecond;
            String resultDate = DateUtil.format(new Date(millisecond),"yyyy-MM-dd");
            Date gtTime = DateUtil.parse(resultDate);

            map.put("gtTime",gtTime);
            map.put("ltTime",now);
            System.out.println("被添加了");

        }else if(selectDate.getTimeState().equals("0")){

            if (selectDate.getStartTime() != null && !selectDate.getStartTime().trim().equals("")){
                System.out.println("start : " + selectDate.getStartTime());
                map.put("startTime", DateUtil.parse(selectDate.getStartTime(),"yyyy-MM-dd"));
            }
            if (selectDate.getEndTime() != null && !selectDate.getEndTime().trim().equals("")){
                System.out.println("end : " + selectDate.getEndTime());

                map.put("endTime", DateUtil.parse(selectDate.getEndTime(),"yyyy-MM-dd"));
            }
        }
        paAct.setCount(activationRecordService.count(map));
        map.put("order","a_createTime " + paAct.getState());
        paAct.setItems(activationRecordService.query(map));

        model.addAttribute("paAct",paAct);
        model.addAttribute("sed",selectDate);

        return "/activationRecord/selectList";
    }

    @RequestMapping("view")
    public String view(Model model, ActivationRecord activationRecord){
        System.out.println();
        System.out.println(">>>>   activationRecord view");
        System.out.println("------------------------------------------");

        List<ActivationRecord> activationRecordList = activationRecordService.find(activationRecord);
        model.addAttribute("activationRecordView",activationRecordList.get(0));

        return "/provider/detail";
    }


//    @RequestMapping("")

}









